package com.cat.model.web.action.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.alonew.core.commons.util.CommonUtil;
import com.cat.model.common.model.admin.AdminUser;
import com.cat.model.common.model.base.Module;
import com.cat.model.web.action.base.BaseAction;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 2820359414533485571L;
	
	private String name;
	private String  password;
	private String error;

	public String home(){
		return "home";
	}
	
	public String logout() {
		HttpSession session = request.getSession();
		session.removeAttribute("user"); 
		session.removeAttribute("login_user");
		return "home";
	}
	
	public String index() {
		showMenu();
		return "index";
	}
	
	public String submit(){
//		AdminUser admin = DaoFactory.getAdminUserDAO().getAdminUserByLoginName(name);
		AdminUser adminUser = new AdminUser();
		adminUser.setPassword(CommonUtil.md5("123456"));
//		AdminUser adminUser = LogicFactory.getAdminLogic().getAdminUserByName(name);
		
		String md5Password = CommonUtil.md5(password);
		if(adminUser == null || !md5Password.equals(adminUser.getPassword())){
			error = "用户名或密码错误！";
			return home();
		}
		request.getSession().setAttribute("user", name);
		request.getSession().setAttribute("login_user", adminUser);
		
		showMenu();
		
		String toUrl = request.getParameter("to_url");
		if(StringUtils.isEmpty(toUrl)) {
			return "index";
		}
		try {
			response.sendRedirect(toUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void showMenu() {
		Module menu1 = new Module();
		menu1.setName("模特信息管理");
		menu1.setUrl("/admin/modelInfo/list");
		menu1.setSn("modeInfo-list");
		menu1.setId(1l);
		
		Module menu2 = new Module();
		menu2.setName("通用配置后台管理");
		menu2.setUrl("/admin/basConfig/list");
		menu2.setSn("basConfig-list");
		menu2.setId(2l);
		
		Module menu3 = new Module();
		menu3.setName("经纪公司管理");
		menu3.setUrl("/admin/companyInfo/list");
		menu3.setSn("companyInfo-list");
		menu3.setId(3l);
		
		Module menu4 = new Module();
		menu4.setName("首页模特推荐");
		menu4.setUrl("/admin/indexRecom/list");
		menu4.setSn("indexRecom-list");
		menu4.setId(4l);
		
		Module menu5 = new Module();
		menu5.setName("广告位管理");
		menu5.setUrl("/admin/banner/list");
		menu5.setSn("banner-list");
		menu5.setId(5l);
		
		/*Module menu6 = new Module();
		menu6.setName("专辑管理");
		menu6.setUrl("/admin/album/list");
		menu6.setSn("album-list");
		menu6.setId(6l);*/
		
		Module menu7 = new Module();
		menu7.setName("用户认证信息审核查询");
		menu7.setUrl("/admin/userAuthInfo/list");
		menu7.setSn("userAuthInfo-list");
		menu7.setId(7l);
		
		Module menu8 = new Module();
		menu8.setName("预约单查询");
		menu8.setUrl("/admin/reservation/list");
		menu8.setSn("reservation-list");
		menu8.setId(8l);
		
		Module menu9 = new Module();
		menu9.setName("用户账户查询");
		menu9.setUrl("/admin/account/list");
		menu9.setSn("account-list");
		menu9.setId(9l);
		
		Module menu10 = new Module();
		menu10.setName("账户提现申请查询");
		menu10.setUrl("/admin/acntWithdraw/list");
		menu10.setSn("acntWithdraw-list");
		menu10.setId(10l);
		
		Module menu11 = new Module();
		menu11.setName("意见反馈查询");
		menu11.setUrl("/admin/suggestion/list");
		menu11.setSn("suggestion-list");
		menu11.setId(11l);
		
		Module menu12 = new Module();
		menu12.setName("模特订单查询");
		menu12.setUrl("/admin/modelOrder/list");
		menu12.setSn("modelOrder-list");
		menu12.setId(12l);
		
		Module menu13 = new Module();
		menu13.setName("模特加时订单查询");
		menu13.setUrl("/admin/extendOrder/list");
		menu13.setSn("extendOrder-list");
		menu13.setId(13l);
		
		Module menu14 = new Module();
		menu14.setName("用户管理");
		menu14.setUrl("/admin/admin/list");
		menu14.setSn("admin-list");
		menu14.setId(14l);
		
		Module menu15 = new Module();
		menu15.setName("菜单管理");
		menu15.setUrl("/admin/adminModule/list");
		menu15.setSn("adminModule-list");
		menu15.setId(15l);
		
		Module menu16 = new Module();
		menu16.setName("角色管理");
		menu16.setUrl("/admin/adminRole/roleList");
		menu16.setSn("adminRole-roleList");
		menu16.setId(16l);
		
		Module menu17 = new Module();
		menu17.setName("自由摄影师/摄影助理管理");
		menu17.setUrl("/admin/photographerInfo/list");
		menu17.setSn("photographerInfo-list");
		menu17.setId(17l);
		
		Module menu18 = new Module();
		menu18.setName("服装搭配师/化妆师/造型师管理");
		menu18.setUrl("/admin/makeupInfo/list");
		menu18.setSn("makeupInfo-list");
		menu18.setId(18l);
		
		Module menu19 = new Module();
		menu19.setName("脚本创作/现场美指管理");
		menu19.setUrl("/admin/dressupInfo/list");
		menu19.setSn("dressupInfo-list");
		menu19.setId(19l);
		
		Module menu20 = new Module();
		menu20.setName("修图师/后期制作管理");
		menu20.setUrl("/admin/graphicsInfo/list");
		menu20.setSn("graphicsInfo-list");
		menu20.setId(20l);
		
		Module menu21 = new Module();
		menu21.setName("摄影师订单查询");
		menu21.setUrl("/admin/photographerOrder/list");
		menu21.setSn("photographerOrder-list");
		menu21.setId(21l);
		
		Module menu22 = new Module();
		menu22.setName("化妆师订单查询");
		menu22.setUrl("/admin/makeupOrder/list");
		menu22.setSn("makeupOrder-list");
		menu22.setId(22l);
		
		Module menu23 = new Module();
		menu23.setName("服装搭配订单查询");
		menu23.setUrl("/admin/dressupOrder/list");
		menu23.setSn("dressupOrder-list");
		menu23.setId(23l);
		
		Module menu24 = new Module();
		menu24.setName("修图订单查询");
		menu24.setUrl("/admin/graphicsOrder/list");
		menu24.setSn("graphicsOrder-list");
		menu24.setId(24l);
		
		Module menu25 = new Module();
		menu25.setName("专访管理");
		menu25.setUrl("/admin/interview/list");
		menu25.setSn("interview-list");
		menu25.setId(25l);
		
		List<Module> menus = new ArrayList<Module>();
		menus.add(menu1);
		menus.add(menu3);
		menus.add(menu4);
		menus.add(menu5);
		//menus.add(menu6);
		menus.add(menu7);
		menus.add(menu8);
		menus.add(menu9);
		menus.add(menu10);
		menus.add(menu11);
		menus.add(menu17);
		menus.add(menu18);
		menus.add(menu19);
		menus.add(menu20);
		menus.add(menu25);

		Module module1 = new Module();
		module1.setName("业务管理");
		module1.setChildren(menus);
		
		List<Module> menus2 = new ArrayList<Module>();
		menus2.add(menu2);
		menus2.add(menu14);
		menus2.add(menu15);
		menus2.add(menu16);
		
		Module module2 = new Module();
		module2.setName("系统管理");
		module2.setChildren(menus2);
		
		List<Module> menus3 = new ArrayList<Module>();
		menus3.add(menu12);
		menus3.add(menu13);
//		menus3.add(menu21);
//		menus3.add(menu22);
//		menus3.add(menu23);
//		menus3.add(menu24);
		
		Module module3 = new Module();
		module3.setName("订单管理");
		module3.setChildren(menus3);
		
		List<Module> children = new ArrayList<Module>();
		children.add(module1);
		children.add(module3);
		children.add(module2);
		
		Module menuModule = new Module();
		menuModule.setChildren(children);
		
		
		
		request.setAttribute("menuModule", menuModule);
	}
	
	public String getError() {
		return error;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
}
